package com.oak.broke.model.vip.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oak.broke.constant.VipConstant;
import com.oak.broke.model.vip.service.IVipApplyService;
import com.oak.broke.model.vip.service.IVipApplyUserService;
import com.oak.broke.model.vip.dao.VipApplyUserDao;
import com.oak.broke.model.vip.dto.VipApplyDTO;
import com.oak.broke.model.vip.entity.VipApplyEntity;
import com.oak.broke.model.vip.entity.VipApplyUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员申请详情
 *
 *
 * @author guoxy
 * @email broke@gmail.com
 * @date 2023-05-18 10:37:20
 */
@Service("vipApplyUserService")
public class VipApplyUserServiceImpl extends ServiceImpl<VipApplyUserDao, VipApplyUserEntity> implements IVipApplyUserService {


    @Autowired
    private IVipApplyService vipApplyService;

    @Override
    public List<VipApplyUserEntity> vipApplyUserList(VipApplyDTO dto) {
        LambdaQueryWrapper<VipApplyUserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(VipApplyUserEntity::getVipApplyUserId,VipApplyUserEntity::getVipApplyId,VipApplyUserEntity::getUserName,
                VipApplyUserEntity::getPhone,VipApplyUserEntity::getFinishTime,VipApplyUserEntity::getApplyTime,VipApplyUserEntity::getIsFinish,VipApplyUserEntity::getRemark);

        queryWrapper.eq(StrUtil.isNotBlank(dto.getIsFinish()),VipApplyUserEntity::getIsFinish,dto.getIsFinish());
        queryWrapper.eq(StrUtil.isNotBlank(dto.getVipApplyId()),VipApplyUserEntity::getVipApplyId,dto.getVipApplyId());
        queryWrapper.orderByDesc(VipApplyUserEntity::getApplyTime);
        List<VipApplyUserEntity> list = this.list(queryWrapper);
        list.forEach(item->{
            item.setIsFinish(VipConstant.VIP_APPLY_FINISH_YES.equals(item.getIsFinish()) ? VipConstant.VIP_YES : VipConstant.VIP_NO);
        });
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveApplyUser(List<VipApplyDTO> list) {
        List<VipApplyUserEntity> updateList = new ArrayList<>();
        list.forEach(item->{
            VipApplyUserEntity entity = new VipApplyUserEntity();
            BeanUtil.copyProperties(item,entity,false);
            entity.setIsFinish(VipConstant.VIP_APPLY_FINISH_YES);
            entity.setFinishTime(DateUtil.date());
            updateList.add(entity);
        });
        this.updateBatchById(updateList);
        // 查询若此申请记录详情下是否有未处理的记录
        String vipApplyId = list.get(0).getVipApplyId();
        VipApplyDTO dto = new VipApplyDTO();
        dto.setIsFinish(VipConstant.VIP_APPLY_FINISH_NO);
        dto.setVipApplyId(vipApplyId);
        List<VipApplyUserEntity> noFinishList = vipApplyUserList(dto);
        if(CollUtil.isEmpty(noFinishList)){
            return;
        }
        // 更新主表未处理人字段
        VipApplyUserEntity applyUserEntity = noFinishList.get(0);
        VipApplyEntity applyEntity = new VipApplyEntity();
        applyEntity.setVipApplyId(applyUserEntity.getVipApplyId());
        applyEntity.setVipApplyUserId(applyUserEntity.getVipApplyUserId());
        vipApplyService.updateById(applyEntity);
    }
}
